package org.ofbiz.autoconfig.auto;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.UtilMisc;
import org.ofbiz.base.util.UtilValidate;
import org.ofbiz.entity.Delegator;
import org.ofbiz.entity.GenericEntityException;
import org.ofbiz.entity.GenericValue;
import org.ofbiz.entity.util.EntityUtil;
import org.ofbiz.service.DispatchContext;
import org.ofbiz.service.GenericServiceException;
import org.ofbiz.service.LocalDispatcher;
import org.ofbiz.service.ServiceUtil;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import javolution.util.FastList;
import javolution.util.FastMap;

public class AutoServices {
    public static final String module = AutoServices.class.getName();
    public static final String OFBIZ_HOME = System.getProperty("ofbiz.home");

   
    @SuppressWarnings("unchecked")
    public static Map<String, Object> autoApiListEntity(DispatchContext dctx, Map<String, ? extends Object> context) {

	Map<String, Object> result = ServiceUtil.returnSuccess();
	LocalDispatcher dispatcher = dctx.getDispatcher();
	Delegator delegator = dctx.getDelegator();
	Map<String, Object> serviceContext = FastMap.newInstance();

	String entityName = (String) context.get("entityName");
	String autoHeadId = (String) context.get("autoHeadId");
	String column = (String) context.get("column");
	String order = (String) context.get("order");
	String fields = (String) context.get("field");
	String superQueryParams = (String) context.get("superQueryParams");
	String superQueryMatchType = (String) context.get("superQueryMatchType");
	try {

	    Map<String, Object> inputFields = (Map<String, Object>) context.get("inputFields");
	    Debug.log("inputFields = " + inputFields);
	    String noConditionFind = "Y";

	    if (UtilValidate.isNotEmpty(inputFields)) {
		noConditionFind = (String) inputFields.get("noConditionFind");
	    } else {
		inputFields = FastMap.newInstance();
		inputFields.put("noConditionFind", "Y");
	    }

	    if (UtilValidate.isNotEmpty(inputFields)) {
		inputFields = resultField(inputFields);
	    }

	    inputFields = AutoUtils.removeOtherField(inputFields, entityName);
	    if (UtilValidate.isNotEmpty(inputFields)) {
		noConditionFind = (String) inputFields.get("noConditionFind");
	    } else {
		inputFields = FastMap.newInstance();
		inputFields.put("noConditionFind", "Y");
	    }
//	    Map<String,Integer> fieldMap = FastMap.newInstance();
	    if (UtilValidate.isNotEmpty(autoHeadId)) {
		GenericValue headEntity = delegator.findOne("OnlScFormHead", UtilMisc.toMap("id", autoHeadId), false);

		if (UtilValidate.isEmpty(entityName)) {
		    entityName = headEntity.getString("tableName");

		}
		if (UtilValidate.isNotEmpty(superQueryParams)) {
		    String queryParams = java.net.URLDecoder.decode(superQueryParams, "utf-8");
		    JSONArray queryList = JSONArray.parseArray(queryParams);
		    for (Object obj : queryList) {
			JSONObject query = JSONObject.parseObject(String.valueOf(obj));
			String rule = query.getString("rule");
			// 文本框,密码框,下拉框,单选框,多选框,开关,日期(年月日),日期(年月日时分秒),日期(时分秒),文件,图片,多行文本,下拉多选框,下拉搜索框,Popup弹框,分类字典树,部门选择,用户选择,富文本,markdown,省市区组件,联动组件,自定义树控件
			// String type = query.getString("type");
			String val = query.getString("val");
			String field = query.getString("field");
			if (UtilValidate.isEmpty(val)) {
			    continue;
			}
			// inputFields
			List<GenericValue> fieldList = delegator.findByAnd("OnlScFormField",
				UtilMisc.toMap("scHeadId", autoHeadId, "dbFieldName", field), null, false);
			if (UtilValidate.isEmpty(fieldList)) {
			    continue;
			}
			// String Integer Double Date BigDecimal Text Blob
			String dbType = EntityUtil.getFirst(fieldList).getString("dbType");
			int index = 0;
			index = getIndex(inputFields, fields, index);
			;
//			    if(UtilValidate.isNotEmpty(fieldMap.get(field))) {
//				index = fieldMap.get(field);
//			    }else {
//				index = 0;
//			    }
			// 条件 : eq 等于 like 包含 right_like 以其开始 left_like 以其结尾 in 在..中 ne 不等于 gt 大于 ge
			// 大于等于 lt 小于 le 小于等于

			if (dbType.equals("String") || dbType.equals("Text") || dbType.equals("Blob")) {
			    inputFields.put(field + "_fld" + index + "_value", val);
			} else if (dbType.equals("Numeric")) {
			    inputFields.put(field + "_fld" + index + "_value", Long.valueOf(val));
			} else if (dbType.equals("Date")) {
			    inputFields.put(field + "_fld" + index + "_value", Timestamp.valueOf(val));
			} else if (dbType.equals("BigDecimal")) {
			    inputFields.put(field + "_fld" + index + "_value", BigDecimal.valueOf(Double.valueOf(val)));
			}

			if (rule.equals("like")) {
			    inputFields.put(field + "_fld" + index + "_op", "contains");
			} else if (rule.equals("eq")) {
			    inputFields.put(field + "_fld" + index + "_op", "equals");
			} else if (rule.equals("ne")) {
			    inputFields.put(field + "_fld" + index + "_op", "notEqual");
			} else if (rule.equals("gt")) {
			    inputFields.put(field + "_fld" + index + "_op", "greaterThan");
			} else if (rule.equals("ge")) {
			    inputFields.put(field + "_fld" + index + "_op", "greaterThanEqualTo");
			} else if (rule.equals("lt")) {
			    inputFields.put(field + "_fld" + index + "_op", "lessThan");
			} else if (rule.equals("le")) {
			    inputFields.put(field + "_fld" + index + "_op", "lessThanEqualTo");
			}
//			    fieldMap.put("field", index + 1);
		    }
		}
	    }

	    String orderBy = "";
	    if (UtilValidate.isNotEmpty(column)) {
		orderBy = column;
	    }
	    if (UtilValidate.isNotEmpty(order)) {
		if (order.equals("desc")) {
		    orderBy = "-" + orderBy;
		}
	    }
	    Integer viewIndex = 0;
	    Integer viewSize = 20;
	    if (UtilValidate.isNotEmpty(context.get("pageNo"))) {
		viewIndex = (Integer) context.get("pageNo") - 1;
	    }

	    if (UtilValidate.isNotEmpty(context.get("pageSize"))) {
		viewSize = (Integer) context.get("pageSize");
	    }

	    if (UtilValidate.isEmpty(noConditionFind)) {
		noConditionFind = "Y";
		inputFields.put("noConditionFind", "Y");
	    }

	    serviceContext.put("inputFields", inputFields);
	    serviceContext.put("entityName", entityName);
	    serviceContext.put("orderBy", orderBy);
	    serviceContext.put("viewIndex", viewIndex);
	    serviceContext.put("viewSize", viewSize);
	    serviceContext.put("noConditionFind", noConditionFind);

	    Map<String, Object> serviceResults = dispatcher.runSync("performFindList", serviceContext);
	    List<GenericValue> list = (List<GenericValue>) serviceResults.get("list");
	    Integer listSize = (Integer) serviceResults.get("listSize");
	    int pages = (int) Math.ceil(listSize / viewSize);

	    List<Map<String, Object>> records = FastList.newInstance();
	    for (GenericValue item : list) {
		Map<String, Object> map = item.getAllFields();
		if (entityName.equals("OnlScFormHead")) {
		    if (UtilValidate.isNotEmpty(item.get("autoScroll"))) {
			map.put("scroll", String.valueOf(item.get("autoScroll")));
			map.put("autoScroll", String.valueOf(item.get("autoScroll")));
		    }
		    if (UtilValidate.isNotEmpty(item.get("relationType"))) {
			map.put("relationType", String.valueOf(item.get("relationType")));
		    }
		}
		records.add(map);
	    }

	    result.put("records", records);
	    result.put("total", listSize);
	    result.put("current", context.get("pageNo"));
	    result.put("size", context.get("pageSize"));
	    result.put("pages", pages);
	} catch (GenericServiceException e) {
	    Debug.logError(e.getMessage(), module);
	} catch (UnsupportedEncodingException e) {
	    Debug.logError(e.getMessage(), module);
	} catch (GenericEntityException e) {
	    Debug.logError(e.getMessage(), module);
	}
	return result;
    }

    public static Map<String, Object> autoGetData(DispatchContext dctx, Map<String, ? extends Object> context) {

	Map<String, Object> result = ServiceUtil.returnSuccess();
	LocalDispatcher dispatcher = dctx.getDispatcher();
	Delegator delegator = dctx.getDelegator();
	Map<String, Object> serviceContext = FastMap.newInstance();

	String entityName = (String) context.get("entityName");
	String autoHeadId = (String) context.get("autoHeadId");
	String column = (String) context.get("column");
	String order = (String) context.get("order");
	String fields = (String) context.get("field");
	String superQueryParams = (String) context.get("superQueryParams");
	String superQueryMatchType = (String) context.get("superQueryMatchType");
	try {

	    Map<String, Object> inputFields = (Map<String, Object>) context.get("inputFields");
	    Debug.log("inputFields = " + inputFields);
	    String noConditionFind = "Y";
	    if (UtilValidate.isNotEmpty(inputFields)) {
		noConditionFind = (String) inputFields.get("noConditionFind");
	    } else {
		inputFields = FastMap.newInstance();
		inputFields.put("noConditionFind", "Y");
	    }

	    if (UtilValidate.isNotEmpty(inputFields)) {
		inputFields = resultField(inputFields);
	    }

	    GenericValue headEntity = delegator.findOne("OnlScFormHead", UtilMisc.toMap("id", autoHeadId), false);

	    if (UtilValidate.isEmpty(entityName)) {
		entityName = headEntity.getString("tableName");
	    }

	    inputFields = AutoUtils.removeOtherField(inputFields, entityName);
//	    Map<String,Integer> fieldMap = FastMap.newInstance();
	    if (UtilValidate.isNotEmpty(autoHeadId)) {

		if (UtilValidate.isNotEmpty(superQueryParams)) {
		    String queryParams = java.net.URLDecoder.decode(superQueryParams, "utf-8");
		    JSONArray queryList = JSONArray.parseArray(queryParams);
		    for (Object obj : queryList) {
			JSONObject query = JSONObject.parseObject(String.valueOf(obj));
			String rule = query.getString("rule");
			// 文本框,密码框,下拉框,单选框,多选框,开关,日期(年月日),日期(年月日时分秒),日期(时分秒),文件,图片,多行文本,下拉多选框,下拉搜索框,Popup弹框,分类字典树,部门选择,用户选择,富文本,markdown,省市区组件,联动组件,自定义树控件
			// String type = query.getString("type");
			String val = query.getString("val");
			String field = query.getString("field");
			if (UtilValidate.isEmpty(val)) {
			    continue;
			}
			// inputFields
			List<GenericValue> fieldList = delegator.findByAnd("OnlScFormField",
				UtilMisc.toMap("scHeadId", autoHeadId, "dbFieldName", field), null, false);
			if (UtilValidate.isEmpty(fieldList)) {
			    continue;
			}
			// String Integer Double Date BigDecimal Text Blob
			String dbType = EntityUtil.getFirst(fieldList).getString("dbType");
			int index = 0;
			index = getIndex(inputFields, fields, index);
			;
//			    if(UtilValidate.isNotEmpty(fieldMap.get(field))) {
//				index = fieldMap.get(field);
//			    }else {
//				index = 0;
//			    }
			// 条件 : eq 等于 like 包含 right_like 以其开始 left_like 以其结尾 in 在..中 ne 不等于 gt 大于 ge
			// 大于等于 lt 小于 le 小于等于

			if (dbType.equals("String") || dbType.equals("Text") || dbType.equals("Blob")) {
			    inputFields.put(field + "_fld" + index + "_value", val);
			} else if (dbType.equals("Numeric")) {
			    inputFields.put(field + "_fld" + index + "_value", Long.valueOf(val));
			} else if (dbType.equals("Date")) {
			    inputFields.put(field + "_fld" + index + "_value", Timestamp.valueOf(val));
			} else if (dbType.equals("BigDecimal")) {
			    inputFields.put(field + "_fld" + index + "_value", BigDecimal.valueOf(Double.valueOf(val)));
			}

			if (rule.equals("like")) {
			    inputFields.put(field + "_fld" + index + "_op", "contains");
			} else if (rule.equals("eq")) {
			    inputFields.put(field + "_fld" + index + "_op", "equals");
			} else if (rule.equals("ne")) {
			    inputFields.put(field + "_fld" + index + "_op", "notEqual");
			} else if (rule.equals("gt")) {
			    inputFields.put(field + "_fld" + index + "_op", "greaterThan");
			} else if (rule.equals("ge")) {
			    inputFields.put(field + "_fld" + index + "_op", "greaterThanEqualTo");
			} else if (rule.equals("lt")) {
			    inputFields.put(field + "_fld" + index + "_op", "lessThan");
			} else if (rule.equals("le")) {
			    inputFields.put(field + "_fld" + index + "_op", "lessThanEqualTo");
			}
//			    fieldMap.put("field", index + 1);
		    }
		}
	    }

	    String orderBy = "";
	    if (UtilValidate.isNotEmpty(column)) {
		if (column.equals("createTime")) {
		    column = "id";
		}
		orderBy = column;
	    }
	    if (UtilValidate.isNotEmpty(order)) {
		if (order.equals("desc")) {
		    orderBy = "-" + orderBy;
		}
	    }
	    Integer viewIndex = 0;
	    Integer viewSize = 20;
	    if (UtilValidate.isNotEmpty(context.get("pageNo"))) {
		viewIndex = (Integer) context.get("pageNo") - 1;
	    }

	    if (UtilValidate.isNotEmpty(context.get("pageSize"))) {
		viewSize = (Integer) context.get("pageSize");
	    }

	    if (UtilValidate.isEmpty(noConditionFind)) {
		noConditionFind = "Y";
		inputFields.put("noConditionFind", "Y");
	    }

	    serviceContext.put("inputFields", inputFields);
	    serviceContext.put("entityName", entityName);
	    serviceContext.put("orderBy", orderBy);
	    serviceContext.put("viewIndex", viewIndex);
	    serviceContext.put("viewSize", viewSize);
	    serviceContext.put("noConditionFind", noConditionFind);

	    Map<String, Object> serviceResults = dispatcher.runSync("performFindList", serviceContext);
	    List<GenericValue> list = (List<GenericValue>) serviceResults.get("list");
	    Integer listSize = (Integer) serviceResults.get("listSize");
	    int pages = (int) Math.ceil(listSize / viewSize);

	    List<Map<String, Object>> records = FastList.newInstance();
	    for (GenericValue item : list) {
		Map<String, Object> map = item;
		records.add(map);
	    }
	    // 如果字段是下拉类型,则增加字段xxxx_dictText
//	    List<GenericValue> fieldList = delegator.findByAnd("OnlScFormHead", UtilMisc.toMap("cgformHeadId", autoHeadId), null,false);
//	    for(GenericValue field : fieldList) {
//		if(UtilValidate.isNotEmpty(field.getString("dictTable"))) {
//		    
//		}
//	    }
	    result.put("records", records);
	    result.put("total", listSize);
	    result.put("current", context.get("pageNo"));
	    result.put("size", context.get("pageSize"));
	    result.put("pages", pages);
	} catch (GenericServiceException e) {
	    Debug.logError(e.getMessage(), module);
	} catch (UnsupportedEncodingException e) {
	    Debug.logError(e.getMessage(), module);
	} catch (GenericEntityException e) {
	    Debug.logError(e.getMessage(), module);
	}
	return result;
    }

    public static Map<String, Object> autoGetTreeData(DispatchContext dctx, Map<String, ? extends Object> context) {
	Map<String, Object> result = ServiceUtil.returnSuccess();
	LocalDispatcher dispatcher = dctx.getDispatcher();
	Delegator delegator = dctx.getDelegator();
	Map<String, Object> serviceContext = FastMap.newInstance();
	String entityName = (String) context.get("entityName");
	String autoHeadId = (String) context.get("autoHeadId");
	String column = (String) context.get("column");
	String order = (String) context.get("order");
//	String fields = (String) context.get("field");
	try {
	    Map<String, Object> inputFields = (Map<String, Object>) context.get("inputFields");
	    Debug.log("inputFields = " + inputFields);
	    String noConditionFind = "Y";
	    if (UtilValidate.isNotEmpty(inputFields)) {
		noConditionFind = (String) inputFields.get("noConditionFind");
	    } else {
		inputFields = FastMap.newInstance();
		inputFields.put("noConditionFind", "Y");
	    }

	    if (UtilValidate.isNotEmpty(inputFields)) {
		inputFields = resultField(inputFields);
	    }

	    GenericValue headEntity = delegator.findOne("OnlScFormHead", UtilMisc.toMap("id", autoHeadId), false);
	    if (UtilValidate.isEmpty(entityName)) {
		entityName = headEntity.getString("tableName");
	    }
	    inputFields = AutoUtils.removeOtherField(inputFields, entityName);

	    String orderBy = "";
	    if (UtilValidate.isNotEmpty(column)) {
		orderBy = column;
	    }
	    if (UtilValidate.isNotEmpty(order)) {
		if (order.equals("desc")) {
		    orderBy = "-" + orderBy;
		}
	    }
	    Integer viewIndex = 0;
	    Integer viewSize = 20;
	    if (UtilValidate.isNotEmpty(context.get("pageNo"))) {
		viewIndex = (Integer) context.get("pageNo") - 1;
	    }

	    if (UtilValidate.isNotEmpty(context.get("pageSize"))) {
		viewSize = (Integer) context.get("pageSize");
	    }

	    if (UtilValidate.isEmpty(noConditionFind)) {
		noConditionFind = "Y";
		inputFields.put("noConditionFind", "Y");
	    }

	    serviceContext.put("inputFields", inputFields);
	    serviceContext.put("entityName", entityName);
	    serviceContext.put("orderBy", orderBy);
	    serviceContext.put("viewIndex", viewIndex);
	    serviceContext.put("viewSize", viewSize);
	    serviceContext.put("noConditionFind", noConditionFind);

	    Map<String, Object> serviceResults = dispatcher.runSync("performFindList", serviceContext);
	    List<GenericValue> list = (List<GenericValue>) serviceResults.get("list");
	    Integer listSize = (Integer) serviceResults.get("listSize");
	    int pages = (int) Math.ceil(listSize / viewSize);

	    List<Map<String, Object>> records = FastList.newInstance();
	    for (GenericValue item : list) {
		Map<String, Object> map = item;
//		if(UtilValidate.isNotEmpty(map.get("haschild"))) {
//		    map.put("hasChild", map.get("haschild"));
//		}
		records.add(map);
	    }
	    result.put("records", records);
	    result.put("total", listSize);
	    result.put("current", context.get("pageNo"));
	    result.put("size", context.get("pageSize"));
	    result.put("pages", pages);
	} catch (GenericServiceException e) {
	    Debug.logError(e.getMessage(), module);
	} catch (GenericEntityException e) {
	    Debug.logError(e.getMessage(), module);
	}
	return result;
    }

    public static Map<String, Object> headNormal(DispatchContext dctx, Map<String, ? extends Object> context) {

	Map<String, Object> result = ServiceUtil.returnSuccess();
//   	LocalDispatcher dispatcher = dctx.getDispatcher();
	Delegator delegator = dctx.getDelegator();
	String autoHeadId = (String) context.get("autoHeadId");
	try {
	    GenericValue entity = delegator.findOne("OnlScFormHead", false, UtilMisc.toMap("id", autoHeadId));
	    entity.set("isDbSynch", "Y");
	    entity.store();

	    if (entity.getString("tableType").equals("3")) {
		List<GenericValue> list = delegator.findByAnd("OnlScFormField", UtilMisc.toMap("id", autoHeadId),
			null, false);
		for (GenericValue item : list) {
		    if (UtilValidate.isNotEmpty(item.get("mainTable"))) {
			List<GenericValue> headList = delegator.findByAnd("OnlScFormHead",
				UtilMisc.toMap("tableName", item.getString("mainTable"), "copyType", 0), null, false);
			GenericValue headInfo = EntityUtil.getFirst(headList);

			List<GenericValue> mainAllList = delegator.findByAnd("OnlScFormHeadAndField",
				UtilMisc.toMap("isDbSynch", "Y", "tableType", "3", "mainTable",
					item.getString("mainTable"), "copyType", 0),
				null, false);
			String subTableStr = headInfo.getString("subTableStr");
			if (UtilValidate.isEmpty(subTableStr))
			    subTableStr = "";
			for (GenericValue itemMain : mainAllList) {
			    subTableStr = subTableStr + itemMain.getString("tableName") + ",";
			}
			if (subTableStr.length() > 1) {
			    subTableStr = subTableStr.substring(0, subTableStr.length() - 1);
			}
			headInfo.set("subTableStr", subTableStr);
			headInfo.store();
		    }
		}
	    }

	    // 数据库同步服务
	} catch (GenericEntityException e) {
	    e.printStackTrace();
	}
	return result;
    }

    public static Map<String, Object> headForce(DispatchContext dctx, Map<String, ? extends Object> context) {

	Map<String, Object> result = ServiceUtil.returnSuccess();
//   	LocalDispatcher dispatcher = dctx.getDispatcher();
	Delegator delegator = dctx.getDelegator();
	String autoHeadId = (String) context.get("autoHeadId");
	try {
	    GenericValue entity = delegator.findOne("OnlScFormHead", false, UtilMisc.toMap("id", autoHeadId));
	    entity.set("isDbSynch", "Y");
	    entity.store();
	    // 强制数据库同步服务,删除旧表,生成新表
	    if (entity.getString("tableType").equals("3")) {
		List<GenericValue> list = delegator.findByAnd("OnlScFormField", UtilMisc.toMap("id", autoHeadId),
			null, false);
		for (GenericValue item : list) {
		    if (UtilValidate.isNotEmpty(item.get("mainTable"))) {
			List<GenericValue> headList = delegator.findByAnd("OnlScFormHead",
				UtilMisc.toMap("tableName", item.getString("mainTable"), "copyType", 0), null, false);
			GenericValue headInfo = EntityUtil.getFirst(headList);

			List<GenericValue> mainAllList = delegator.findByAnd("OnlScFormHeadAndField",
				UtilMisc.toMap("isDbSynch", "Y", "tableType", "3", "mainTable",
					item.getString("mainTable"), "copyType", 0),
				null, false);
			String subTableStr = headInfo.getString("subTableStr");
			if (UtilValidate.isEmpty(subTableStr))
			    subTableStr = "";
			for (GenericValue itemMain : mainAllList) {
			    subTableStr = subTableStr + itemMain.getString("tableName") + ",";
			}
			if (subTableStr.length() > 1) {
			    subTableStr = subTableStr.substring(0, subTableStr.length() - 1);
			}
			headInfo.set("subTableStr", subTableStr);
			headInfo.store();
		    }
		}
	    }
	} catch (GenericEntityException e) {
	    e.printStackTrace();
	}
	return result;
    }

    public static int getIndex(Map<String, Object> inputFields, String field, int index) {
	if (UtilValidate.isNotEmpty(inputFields.get(field))) {
	    index = index + 1;
	}
	for (int i = 0; i < 30; i++) {
	    if (UtilValidate.isNotEmpty(inputFields.get(field + "_fld" + i + "_value"))) {
		index = index + 1;
	    } else {
		break;
	    }
	}
	return index;
    }

    public static Map<String, Object> resultField(Map<String, Object> inputFields) {

	for (Map.Entry<String, Object> entry : inputFields.entrySet()) {
	    if (entry.getValue() instanceof String) {
		String newValue = entry.getValue().toString();
		if (newValue.contains("[") && newValue.contains("]")) {
		    newValue = newValue.replace("[", "").replace("]", "");
		    String[] array = newValue.split(",");
		    List<Object> newList = FastList.newInstance();
		    for (int i = 0; i < array.length; i++) {
			if (UtilValidate.isNotEmpty(array[i])) {
			    newList.add(array[i]);
			}
		    }
		    if (UtilValidate.isNotEmpty(newList)) {
			inputFields.put(entry.getKey(), newList);
		    }
		}
	    }
	}
	return inputFields;
    }

    public static Map<String, Object> executeRuleByCodeBatch(DispatchContext dctx,
	    Map<String, ? extends Object> context) {

	Map<String, Object> result = ServiceUtil.returnSuccess();
//       	LocalDispatcher dispatcher = dctx.getDispatcher();
	Delegator delegator = dctx.getDelegator();
	Map commonFormData = (Map) context.get("commonFormData");

	List rules = (List) context.get("rules");

	Debug.log("commonFormData" + commonFormData);
	Debug.log("rules" + rules);

	result.put("nfsList", FastList.newInstance());
	return result;
    }

    @SuppressWarnings("unchecked")
    public static Map<String, Object> autoCustomData(DispatchContext dctx, Map<String, ? extends Object> context) {

	Map<String, Object> result = ServiceUtil.returnSuccess();
	LocalDispatcher dispatcher = dctx.getDispatcher();
	Delegator delegator = dctx.getDelegator();
	Map<String, Object> serviceContext = FastMap.newInstance();

	String entityName = (String) context.get("entityName");
	String column = (String) context.get("column");
	String order = (String) context.get("order");
	String fields = (String) context.get("field");
	String superQueryParams = (String) context.get("superQueryParams");
	String superQueryMatchType = (String) context.get("superQueryMatchType");
	try {

	    Map<String, Object> inputFields = (Map<String, Object>) context.get("inputFields");
	    Debug.log("inputFields = " + inputFields);
	    String noConditionFind = "Y";
	    if (UtilValidate.isNotEmpty(inputFields)) {
		noConditionFind = (String) inputFields.get("noConditionFind");
	    } else {
		inputFields = FastMap.newInstance();
		inputFields.put("noConditionFind", "Y");
	    }

	    if (UtilValidate.isNotEmpty(inputFields)) {
		inputFields = resultField(inputFields);
	    }
	    inputFields = AutoUtils.removeOtherField(inputFields, entityName);
	    if (UtilValidate.isNotEmpty(superQueryParams)) {
		String queryParams = java.net.URLDecoder.decode(superQueryParams, "utf-8");
		JSONArray queryList = JSONArray.parseArray(queryParams);
		for (Object obj : queryList) {
		    JSONObject query = JSONObject.parseObject(String.valueOf(obj));
		    String rule = query.getString("rule");
		    // 文本框,密码框,下拉框,单选框,多选框,开关,日期(年月日),日期(年月日时分秒),日期(时分秒),文件,图片,多行文本,下拉多选框,下拉搜索框,Popup弹框,分类字典树,部门选择,用户选择,富文本,markdown,省市区组件,联动组件,自定义树控件
		    // String type = query.getString("type");
		    String val = query.getString("val");
		    String field = query.getString("field");
		    if (UtilValidate.isEmpty(val)) {
			continue;
		    }
		    // String Integer Double Date BigDecimal Text Blob
		    int index = 0;
		    index = getIndex(inputFields, fields, index);
		    // 条件 : eq 等于 like 包含 right_like 以其开始 left_like 以其结尾 in 在..中 ne 不等于 gt 大于 ge
		    // 大于等于 lt 小于 le 小于等于

		    String type = AutoUtils.optimizingType(field, entityName);
		    if (type.equals("date-time")) {
			if (!AutoUtils.isValidDate(String.valueOf(val))) {
			    inputFields.put(field + "_fld" + index + "_value", Timestamp.valueOf(val));
			}
		    } else if (type.equals("date") || type.equals("Date")) {
			inputFields.put(field + "_fld" + index + "_value", java.sql.Date.valueOf(val));
		    } else if (type.equals("time")) {
			inputFields.put(field + "_fld" + index + "_value", java.sql.Time.valueOf(val));
		    } else if (type.equals("currency-amount") || type.equals("currency-precise")
			    || type.equals("fixed-point")) {
			inputFields.put(field + "_fld" + index + "_value", BigDecimal.valueOf(Double.valueOf(val)));
		    } else if (type.equals("floating-point")) {
			inputFields.put(field + "_fld" + index + "_value", Double.valueOf(val));
		    } else if (type.equals("numeric")) {
			inputFields.put(field + "_fld" + index + "_value", Long.valueOf(val));
		    } else {
			inputFields.put(field + "_fld" + index + "_value", val);
		    }

		    if (rule.equals("like")) {
			inputFields.put(field + "_fld" + index + "_op", "contains");
		    } else if (rule.equals("eq")) {
			inputFields.put(field + "_fld" + index + "_op", "equals");
		    } else if (rule.equals("ne")) {
			inputFields.put(field + "_fld" + index + "_op", "notEqual");
		    } else if (rule.equals("gt")) {
			inputFields.put(field + "_fld" + index + "_op", "greaterThan");
		    } else if (rule.equals("ge")) {
			inputFields.put(field + "_fld" + index + "_op", "greaterThanEqualTo");
		    } else if (rule.equals("lt")) {
			inputFields.put(field + "_fld" + index + "_op", "lessThan");
		    } else if (rule.equals("le")) {
			inputFields.put(field + "_fld" + index + "_op", "lessThanEqualTo");
		    }
		}
	    }

	    String orderBy = "";
	    if (UtilValidate.isNotEmpty(column)) {
		if (column.equals("createTime")) {
		    column = "id";
		}
		orderBy = column;
	    }
	    if (UtilValidate.isNotEmpty(order)) {
		if (order.equals("desc")) {
		    orderBy = "-" + orderBy;
		}
	    }
	    Integer viewIndex = 0;
	    Integer viewSize = 20;
	    if (UtilValidate.isNotEmpty(context.get("pageNo"))) {
		viewIndex = (Integer) context.get("pageNo") - 1;
	    }

	    if (UtilValidate.isNotEmpty(context.get("pageSize"))) {
		viewSize = (Integer) context.get("pageSize");
	    }

	    if (UtilValidate.isEmpty(noConditionFind)) {
		noConditionFind = "Y";
		inputFields.put("noConditionFind", "Y");
	    }

	    serviceContext.put("inputFields", inputFields);
	    serviceContext.put("entityName", entityName);
	    serviceContext.put("orderBy", orderBy);
	    serviceContext.put("viewIndex", viewIndex);
	    serviceContext.put("viewSize", viewSize);
	    serviceContext.put("noConditionFind", noConditionFind);

	    Map<String, Object> serviceResults = dispatcher.runSync("performFindList", serviceContext);
	    List<GenericValue> list = (List<GenericValue>) serviceResults.get("list");
	    Integer listSize = (Integer) serviceResults.get("listSize");
	    int pages = (int) Math.ceil(listSize / viewSize);

	    List<Map<String, Object>> records = FastList.newInstance();
	    for (GenericValue item : list) {
		Map<String, Object> map = item;
		records.add(map);
	    }
	    // 如果字段是下拉类型,则增加字段xxxx_dictText
//	    List<GenericValue> fieldList = delegator.findByAnd("OnlScFormHead", UtilMisc.toMap("cgformHeadId", autoHeadId), null,false);
//	    for(GenericValue field : fieldList) {
//		if(UtilValidate.isNotEmpty(field.getString("dictTable"))) {
//		    
//		}
//	    }
	    result.put("records", records);
	    result.put("total", listSize);
	    result.put("current", context.get("pageNo"));
	    result.put("size", context.get("pageSize"));
	    result.put("pages", pages);
	} catch (GenericServiceException e) {
	    Debug.logError(e.getMessage(), module);
	} catch (UnsupportedEncodingException e) {
	    Debug.logError(e.getMessage(), module);
	}
	return result;
    }

}
